package com.qf.ts.platform.front.config;

import com.qf.ts.platform.front.realm.MyRealm;
import org.springframework.context.annotation.Bean;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public MyRealm myRealm() {
        return new MyRealm();
    }

    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRealm(myRealm());
        return manager;
    }

    /**
     * @Bean创建一个类对象.@Bean("shiroFilter")
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean() {

        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(securityManager());

//        bean.setLoginUrl("/login.html");
//        bean.setSuccessUrl("/index.html");
//        bean.setUnauthorizedUrl("/error.html");

        // /login.html = anon
        Map<String, String> map = new HashMap<>();
//        map.put("/login.html", "anon");
//        map.put("/**", "anon");
        map.put("**","anon");
//        map.put("/upload.html", "anon");
//        map.put("/templates/**", "anon");
//        map.put("/user/**","anon");
//        map.put("/logout", "logout");
//        map.put("/index.html", "authc");
//        map.put("/**", "authc");
        bean.setFilterChainDefinitionMap(map);

        return bean;
    }
}
